package service;

import java.util.HashMap;
import java.util.List;

import model.Dictionary;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;

import dao.BusiDictDao;
import dao.DictionaryDao;

@Service
public class DictionaryService {
	@Autowired
	private DictionaryDao dictionaryDao;
	@Autowired
	private BusiDictDao busiDictDao;

	public PageList<HashMap<String, Object>> queryPage(int page, int limit,
			String str) {
		PageBounds pageBounds = new PageBounds(page, limit);
		List<HashMap<String, Object>> list;
		if (StringUtils.isNotBlank(str)) {
			list = dictionaryDao.searchPage(pageBounds, str);
		} else {
			list = dictionaryDao.queryPage(pageBounds);
		}
		PageList<HashMap<String, Object>> pageList = (PageList<HashMap<String, Object>>) list;
		return pageList;
	};

	public List<HashMap<String, Object>> listAll() {
		return dictionaryDao.listAll();
	}

	public void saveOrUpdate(Dictionary dictionary) {
		if (dictionary.getId() == null) {
			dictionaryDao.save(dictionary);
		} else {
			dictionaryDao.update(dictionary);
		}
	}

	public void delete(String ids) {
		String[] idStrs = ids.split(",");
		for (String id : idStrs) {
			Integer _id = Integer.parseInt(id);
			// delete Dictionary
			dictionaryDao.delete(_id);
			// delete Businessdic
			busiDictDao.deleteByType(_id);
		}
	}
}
